package cn.onein.edu.web.dao;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.ContentTags;
import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.web.util.SearchParamUtil;

/**
 * 育儿百科查询、Tag和文章 DAO
 * 
 * @author Upton
 * 
 */
@Service
public class LearnContentDao {

	@Autowired
	private SqlMapClientTemplate sqlMapClientTemplate;

	public LearnContent getLearnContentById(Long id) {
		return (LearnContent) sqlMapClientTemplate.queryForObject(
				"getLearnContentById", id);
	}

	public void updateContentHit(Long id) {
		sqlMapClientTemplate.update("updateContentHit", id);
	}

	@SuppressWarnings("unchecked")
	public List<LearnContent> getContentListBytag(String tagword,Integer perPage,Integer beginIndex) {
		List<LearnContent> list = new ArrayList<LearnContent>();
		
		ContentTags contentTags = (ContentTags) sqlMapClientTemplate
				.queryForObject("getContentTagsByTagName", tagword);
		
		if(contentTags != null){
			String ids = contentTags.getContentIds();
			SearchParamUtil param = new SearchParamUtil();
			param.setParam(ids);
			param.setPerPage(perPage);
			param.setBeninIndex(beginIndex);
			list = (List<LearnContent>) sqlMapClientTemplate.queryForList("getContentListBytag", param);
		}
		
		for(LearnContent o : list){
			List<String> tagList = o.getTagList();
			StringBuffer buffer = new StringBuffer();
			for(String tag : tagList){
				String urlEncode = "";
				try {
				   urlEncode = URLEncoder.encode(tag,"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				buffer.append("<a class='a-hot-tags' href="+urlEncode+" >"+tag+"</a>&nbsp;&nbsp;");
			}
			o.setTags(buffer.toString());
		}

		return list;
	}
	
	public Integer getContentCountBytag(String tagword) {
		ContentTags contentTags = (ContentTags) sqlMapClientTemplate
				.queryForObject("getContentTagsByTagName", tagword);
		
		if(contentTags != null){
			String ids = contentTags.getContentIds();
			return (Integer)sqlMapClientTemplate.queryForObject("getContentCountByTag", ids);
		}

		return 0;
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getContentListBySearch(SearchParamUtil param) {
	   return (List<LearnContent>) sqlMapClientTemplate.queryForList("getContentListBySearch", param);
	}
	
	public Integer getContentCountBySearch(String search){
		return (Integer)sqlMapClientTemplate.queryForObject("getContentCountBySearch",search);
	}
	
	@SuppressWarnings("unchecked")
	public List<ContentTags> getHotTags(){
		return (List<ContentTags>)sqlMapClientTemplate.queryForList("getHotTags");
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getHotContentByHits(int count) {
        List<LearnContent> result = (List<LearnContent>) sqlMapClientTemplate.queryForList("getHotContentByHits");
		
		if(result != null){
			if(count >= result.size()){
				return result;
			}
			else{
				return result.subList(0, count);
			}				
		}
		else{
			return null;
		}
	}	
}
